home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSFILE1.C < prev    next >
C/C++ Source or Header  |  1995-09-20  |  6KB  |  236 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsfile1.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains .
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        MS Windows NT
  33. //        OS/2 2.X+
  34. //        OS/2 2.0 PM
  35. //        SCO UNIX.
  36. //
  37. //    The following compilers are supported:
  38. //        MSC 6.0A
  39. //        MSC/C++ 7.0
  40. //        Borland C++ 3.1 for DOS
  41. //        Borland C++ 1.0 for OS/2 2.X
  42. //        SCO UNIX cc
  43. //
  44. //----------------------------------------------------------------------------
  45. #include <bs.h>
  46.  
  47.  
  48. //----------------------------------------------------------------------------
  49. //   Description:    Copy data between two files.
  50. //    Parameters:    hf1        File handle 1. 
  51. //                                    File to copy TO
  52. //                        fpos1        Position to start copying at.
  53. //                                    If -1, copy starting at current position.
  54. //                        hf2        File handle 2. 
  55. //                                    File to copy FROM
  56. //                        fpos2        Position to start copying at.
  57. //                                    If -1, copy starting at current position.
  58. //                        flen        Length of file to copy.
  59. //                                    If < 0, copy to end of input file.
  60. //       Returns:    TRUE if successful.
  61. //----------------------------------------------------------------------------
  62. BOOL FN_E FileCopy(HF hf1, FPOS fpos1, HF hf2, FPOS fpos2, FPOS flen)
  63. {
  64.     FPOS fsize;
  65.     SIZET cb;
  66.     PBYTE pbBuf = NULL;
  67.     BOOL fResult = FALSE;
  68.  
  69.     if (fpos1 < 0)
  70.         fpos1 = FileGetPos(hf1);
  71.     if (fpos1 < 0)
  72.         return FALSE;
  73.     if (fpos2 < 0)
  74.         fpos2 = FileGetPos(hf2);
  75.     if (fpos2 < 0)
  76.         return FALSE;
  77.     fsize = FileGetSize(hf2);
  78.     if (fsize < 0)
  79.         return FALSE;
  80.     if (fpos2 >= fsize)                        // Copying from past eof?
  81.         return TRUE;
  82.     if (flen < 0)
  83.         flen = fsize - fpos2;
  84.     flen = MIN(flen, fsize - fpos2);
  85.     if (flen <= 0)                                // Nothing to copy?
  86.         return TRUE;
  87.                                                     // Allocate a copy buffer
  88.     cb = (SIZET)MIN((FPOS)(16 _K), flen);
  89.     pbBuf = MemAlloc(cb);
  90.     if (pbBuf == NULL)
  91.         {
  92.         ErrorNoMem();
  93.         goto ERROR_EXIT;
  94.         }
  95.     while (flen)                                // Copy file
  96.         {
  97.         SIZET cCopy = (SIZET)MIN((FPOS)cb, flen);
  98.  
  99.         if (!FileRead(hf2, pbBuf, cCopy, fpos2))
  100.             goto ERROR_EXIT;
  101.  
  102.         if (!FileWrite(hf1, pbBuf, cCopy, fpos1))
  103.             goto ERROR_EXIT;
  104.  
  105.         flen -= (FPOS)cCopy;
  106.         fpos1 += (FPOS)cCopy;
  107.         fpos2 += (FPOS)cCopy;
  108.         }
  109.  
  110.     fResult = TRUE;
  111. ERROR_EXIT:
  112.     if (pbBuf)
  113.         MemFree(pbBuf);
  114.  
  115.     return fResult;
  116. }
  117.  
  118.  
  119. //----------------------------------------------------------------------------
  120. //   Description:    Compute the CRC of a file.
  121. //    Parameters:    hf        File handle
  122. //                        pcrc    Variable to recieve crc
  123. //                        flen    Length of file to compute CRC for.
  124. //                                Length is adjust to fit file size.
  125. //                                If < 0, calculate remaing length of file.
  126. //                        fpos    Address to start computing at.
  127. //                                If < 0, start at current position.
  128. //       Returns:    TRUE if successful.
  129. //----------------------------------------------------------------------------
  130. BOOL FN_E FileCrc(HF hf, PCRC pcrc, FPOS flen, FPOS fpos)
  131. {
  132.     SIZET cBuf;
  133.     FPOS fsize;
  134.     SIZET cb;
  135.     PBYTE pbBuf = NULL;
  136.     BOOL fResult = FALSE;
  137.     
  138.     Assert(pcrc);
  139.   if (fpos < 0)
  140.         fpos = FileGetPos(hf);
  141.     if (fpos < 0)
  142.         return FALSE;
  143.     fsize = FileGetSize(hf);
  144.     if (fsize < 0)
  145.         return FALSE;
  146.     *pcrc = 0;
  147.     if (fpos >= fsize)                        // Check starting location
  148.         return TRUE;
  149.     if (flen < 0)                                // If < 0, check to end of file
  150.         flen = fsize;
  151.     flen = MIN(flen, fsize - fpos);        // Compute maximum length
  152.  
  153.     cb = (SIZET)MIN((FPOS)(16 _K), flen);
  154.     pbBuf = MemAlloc(cb);
  155.     if (pbBuf == NULL)
  156.         {
  157.         ErrorNoMem();
  158.         goto ERROR_EXIT;
  159.         }
  160.  
  161.     while (flen)
  162.         {
  163.         cBuf = (SIZET)MIN((FPOS)cb, flen);
  164.  
  165.         if (!FileRead(hf, pbBuf, cBuf, fpos))
  166.             goto ERROR_EXIT;
  167.  
  168.         *pcrc = CrcCalcAppend(pbBuf, cBuf, *pcrc);
  169.  
  170.         fpos += (FPOS)cBuf;
  171.         flen -= (FPOS)cBuf;
  172.         }
  173.  
  174.     fResult = TRUE;
  175. ERROR_EXIT:
  176.     if (pbBuf)
  177.         MemFree(pbBuf);
  178.  
  179.     return fResult;
  180. }
  181.  
  182.  
  183. //----------------------------------------------------------------------------
  184. //   Description:    Garble a file
  185. //    Parameters:    hf        File handle
  186. //                        pcsz    String to garble file with
  187. //                        flen    Length of file to compute CRC for.
  188. //                                Length is adjust to fit file size.
  189. //                                If < 0, calculate remaing length of file.
  190. //                        fpos    Address to start computing at.
  191. //                                If < 0, start at current position.
  192. //       Returns:    TRUE if successful.
  193. //----------------------------------------------------------------------------
  194. BOOL FN_E FileGarble(HF hf, PCSZ pcsz, FPOS flen, FPOS fpos)
  195. {
  196.     BYTE bBuf[512];
  197.     SIZET cBuf;
  198.     FPOS fsize;
  199.     SIZET cStr;
  200.  
  201.     Assert(pcsz && pcsz[0]);
  202.     cStr = strlen(pcsz);
  203.     if (fpos < 0)
  204.         fpos = FileGetPos(hf);
  205.     if (fpos < 0)
  206.         return FALSE;
  207.     fsize = FileGetSize(hf);
  208.     if (fsize < 0)
  209.         return FALSE;
  210.     if (fpos >= fsize)                        // Check starting location
  211.         return TRUE;
  212.     if (flen < 0)                                // If < 0, check to end of file
  213.         flen = fsize;
  214.     flen = MIN(flen, fsize - fpos);        // Compute maximum length
  215.     while (flen)
  216.         {
  217.         cBuf = (SIZET)MIN((FPOS)sizeof(bBuf), flen);
  218.  
  219.         if (!FileRead(hf, bBuf, cBuf, fpos))
  220.             return FALSE;
  221.  
  222.         Garble(bBuf, cBuf, (PBYTE)pcsz, cStr);
  223.  
  224.         if (!FileWrite(hf, bBuf, cBuf, fpos))
  225.             return FALSE;
  226.  
  227.         fpos += (FPOS)cBuf;
  228.         flen -= (FPOS)cBuf;
  229.         }
  230.  
  231.     return TRUE;
  232. }
  233. //----------------------------------------------------------------------------
  234. //------------------------------- End of File --------------------------------
  235. //----------------------------------------------------------------------------
  236.